package at.favre.lib.securesharedpreferences.slice;

import at.favre.lib.armadillo.Armadillo;
import at.favre.lib.armadillo.ArmadilloSharedPreferences;
import at.favre.lib.armadillo.SecureSharedPreferenceCryptoException;
import at.favre.lib.securesharedpreferences.ResourceTable;
import at.favre.lib.securesharedpreferences.util.TextUtils;
import at.favre.lib.securesharedpreferences.util.ToastDialogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TextField;
import timber.log.Timber;

import static at.favre.lib.securesharedpreferences.slice.MainAbilitySlice.PREF_NAME;
import static at.favre.lib.securesharedpreferences.slice.MainAbilitySlice.SECRET;

public class ChangePasswordSlice extends AbilitySlice {

    static final String KEY_RESULT = "key_result";

    private TextField tfCurrentPassword;
    private TextField tfNewPassword;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        setUIContent(ResourceTable.Layout_ability_change_password);
        tfCurrentPassword = (TextField) findComponentById(ResourceTable.Id_tf_curr_password);
        tfNewPassword = (TextField) findComponentById(ResourceTable.Id_tf_new_password);
        findComponentById(ResourceTable.Id_btn_confirm).setClickedListener(component -> {
            if (TextUtils.isEmpty(tfCurrentPassword.getText())) {
                Timber.i("Enter current password");
                ToastDialogUtil.showToast(this, "Enter current password");
                return;
            } else if (TextUtils.isEmpty(tfNewPassword.getText())) {
                Timber.i("Enter new password");
                ToastDialogUtil.showToast(this, "Enter new password");
                return;
            }

            // Get current pass
            char[] currentPassword = new char[tfCurrentPassword.getText().length()];
            tfCurrentPassword.getText().getChars(0, tfCurrentPassword.getText().length(), currentPassword, 0);

            // Init Armadillo
            ArmadilloSharedPreferences armadillo = Armadillo.create(this, PREF_NAME)
                    .encryptionFingerprint(this, SECRET)
                    .password(currentPassword)
                    .supportVerifyPassword(true)
                    .build();
            if (!armadillo.isValidPassword()) {
                Timber.i("Incorrect password!");
                ToastDialogUtil.showToast(this, "Incorrect password!");
                return;
            }

            // Get new pass
            char[] newPassword = new char[tfNewPassword.getText().length()];
            tfNewPassword.getText().getChars(0, tfNewPassword.getText().length(), newPassword, 0);

            // Change pass
            try {
                armadillo.changePassword(newPassword);
                ToastDialogUtil.showToast(this, "Password successfully changed!");
                tfCurrentPassword.setText("");
                tfNewPassword.setText("");
                openMainAbilitySlice();
            } catch (SecureSharedPreferenceCryptoException ex) {
                Timber.i("Incorrect password!");
                ToastDialogUtil.showToast(this, "Incorrect password!");
            }
        });
        findComponentById(ResourceTable.Id_appBar_backButton_touchTarget).setClickedListener(component ->
                exit(false));
    }

    private void openMainAbilitySlice() {
        exit(true);
    }

    private void exit(boolean changeSuccess) {
        Intent intent = new Intent();
        intent.setParam(KEY_RESULT, changeSuccess);
        setResult(intent);
        onBackPressed();
    }
}
